package com.liuxl.core.common.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.liuxl.core.mapper.custom.RoleDao;
import com.liuxl.core.model.auto.TsysRole;
import com.liuxl.core.shiro.util.ShiroUtils;
import com.liuxl.core.util.StringUtils;




/**
 * 自定义角色拦截器
 * @author lxl
 *
 */
@Component
public class RoleInterceptor implements HandlerInterceptor{

	@Autowired
	private RoleDao roleDao ;//角色dao
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		Object userid = request.getSession().getAttribute("sessionUserId");
		if(userid!=null) {
			if(!StringUtils.isEmpty(userid+"")) {
				List<TsysRole> tsysRoles= roleDao.queryUserRole(ShiroUtils.getUserId());
				if(tsysRoles!=null) {
					if(tsysRoles.get(0).getName().equals("住建用户")) {  //通过用户角色，控制链接访问权限
						return false;
					}
				}
			}
		}
        return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println(">>>MyInterceptor1>>>>>>>请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
		System.out.println(">>>MyInterceptor1>>>>>>>在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）");
	}
	
}
